package com.sgb.link.view.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.link.R;
import com.sgb.link.entity.PickFilterEntity;
import com.sgb.link.view.adapter.PickFilterWindowAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.util.MToast;
import java.util.ArrayList;
import java.util.List;

public class TwoLevelMultipleChoiceWindow extends BasePopWindow {

    private String title;

    private PickFilterWindowAdapter pickAdapter1;
    private PickFilterWindowAdapter pickAdapter2;
    //private PickFilterSecondWindowAdapter secondPickAdapter;
    private RecyclerView recycler1;
    private RecyclerView recycler2;
    private TextView tvTitle;

    private List<PickFilterEntity> pickFilterEntityList;

    private List<PickFilterEntity> proList = new ArrayList<>();

    //回显
    private List<PickFilterEntity> historyList = new ArrayList<>();

    //回显
    private SparseBooleanArray checkList = new SparseBooleanArray();

    private List<PickFilterEntity<List<PickFilterEntity>>> list;
    private PickFilterEntity<List<PickFilterEntity>> currentHistoryFirstItem;
    private PickFilterEntity<List<PickFilterEntity>> currentFirstItem;
    private int currentPosition = 0;
    private boolean isOneSelete = false;
    private int Positions = 0;

    public TwoLevelMultipleChoiceWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_multiple_choice;
    }

    public void setTitle(String title) {
        this.title = title;
        if (tvTitle != null && !TextUtils.isEmpty(title))
            tvTitle.setText(title);
    }

    @Override
    protected void initView(PopViewHolder holder) {
        recycler1 = holder.getPop().findViewById(R.id.rlv_list);
        recycler2 = holder.getPop().findViewById(R.id.rlv2_list);
        TextView cancel = holder.getPop().findViewById(R.id.cancel);
        tvTitle = holder.getPop().findViewById(R.id.title);
        TextView confirm = holder.getPop().findViewById(R.id.confirm);
        //取消
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
            }
        });

        //确定
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onResultClick != null) {
                    if (!isOneSelete){
                        if (pickFilterEntityList.size() > 0) { //点击
                            onResultClick.result(pickFilterEntityList);
                            dissMiss();
                        } else if (pickAdapter2.getCheckDatas().size() > 0){//回显的数据 点确定
                            onResultClick.result(pickAdapter2.getCheckDatas());
                            dissMiss();
                        } else {
                            MToast.showToast(mContext, "请选择数据后再确认");
                        }
                    }else {
                        //如果二级没有选中那么取一级下面所有二级
                        if (pickAdapter2.getCheckDatas().size() > 0){//回显的数据 点确定
                            onResultClick.result(pickAdapter2.getCheckDatas());
                            dissMiss();
                        } else {
                            List<PickFilterEntity> children = list.get(Positions).getChildren();
                            onResultClick.result(children);
                            dissMiss();
                        }
                    }
                }
            }
        });

        if (pickAdapter1 == null) {
            pickAdapter1 = new PickFilterWindowAdapter(mContext, R.layout.item_pick_filter_child_window, null);
            pickAdapter1.setIsDisplay(false); //勾选隐藏
            pickAdapter1.setItemSelect(true);
            pickAdapter1.setDefItem(0);
        }
        if (pickAdapter2 == null) {
            pickAdapter2 = new PickFilterWindowAdapter(mContext, R.layout.item_pick_filter_child_window_1, null);
            pickAdapter2.setIsDisplay(true); //勾选显示
            pickAdapter2.setMultiSelect(true);
            pickAdapter2.setMost(10);
            pickAdapter2.setDefItem(0);
        }
//        if (secondPickAdapter == null){
//            secondPickAdapter = new PickFilterSecondWindowAdapter(mContext);
//        }

        XLinearLayoutManager manager = new XLinearLayoutManager(mContext);
        recycler1.setLayoutManager(manager);
        recycler1.setAdapter(pickAdapter1);
        XLinearLayoutManager manager1 = new XLinearLayoutManager(mContext);
        recycler2.setLayoutManager(manager1);
        recycler2.setAdapter(pickAdapter2);

        pickAdapter1.setOnItemClick(new AdapterOnItemClick<PickFilterEntity>() {
            @Override
            public void onItemClick(PickFilterEntity entity, int position) {
                currentFirstItem = list.get(position);
                Positions = position;
                setData2(list.get(position).getChildren());
            }
        });

        pickAdapter2.setOnItemMultiClick(new BaseRecycleAdapter.OnItemMultiClick<PickFilterEntity>() {
            @Override
            public void onItemMultiClick(List<PickFilterEntity> pickFilterEntities, int position) {
                pickFilterEntityList.clear();
                pickFilterEntityList.addAll(pickFilterEntities);
            }
        });

        pickFilterEntityList = new ArrayList<>();
    }

    public void setSingleData(List<PickFilterEntity> list){
        setData2(list);
        recycler1.setVisibility(View.GONE);
    }

    public void setSecondMost(int number){
        if (pickAdapter2 != null){
            pickAdapter2.setMost(number);
        }
    }

    public void setSingleData(List<PickFilterEntity> list, List<PickFilterEntity> historyList){//只有二级回显
        setData2(list);
        if (historyList != null && !historyList.isEmpty()){
            setSingleDataTwo(historyList);
        }
        recycler1.setVisibility(View.GONE);
    }

    private void setSingleDataTwo(List<PickFilterEntity> historyList) {
        List<PickFilterEntity> datas = pickAdapter2.getDatas();
        if (datas != null && !datas.isEmpty()){
            for (PickFilterEntity entity : datas){
                if (historyList.contains(entity)){
                    checkList.put(datas.indexOf(entity), true);
                } else {
                    checkList.put(datas.indexOf(entity), false);
                }
            }
            pickAdapter2.setCheckDefultDatas(historyList, checkList);
        }
    }

    public void setData(List<PickFilterEntity<List<PickFilterEntity>>> list) {
        this.list = list;
        List<PickFilterEntity> pickList = new ArrayList<>();
        PickFilterEntity entity;
        for (PickFilterEntity<List<PickFilterEntity>> pickFilterEntity : list) {
            entity = new PickFilterEntity();
            entity.setNo(pickFilterEntity.getNo());
            entity.setName(pickFilterEntity.getName());
            pickList.add(entity);
        }
        pickAdapter1.setDatas(pickList);
        pickAdapter1.setDefItem(currentPosition);
        //recycler1.scrollToPosition(currentPosition);
        currentFirstItem = list.get(currentPosition);
        setData2(list.get(currentPosition).getChildren());
    }

    /**
     * 数据回显
     * @param list
     * @param secondList
     */
    public void setData(List<PickFilterEntity<List<PickFilterEntity>>> list, List<PickFilterEntity> secondList) {
        if (secondList != null && !secondList.isEmpty()) {
            historyList.clear();
            historyList.addAll(secondList);
            //因为多选的一级是固定的 所以取secondList一个就能获取一级
            //选中的一级item
            for (PickFilterEntity<List<PickFilterEntity>> item : list){
                if (item.getChildren().contains(secondList.get(0))){
                    currentHistoryFirstItem = item;
                    currentPosition = list.indexOf(item);
                    break;
                }
            }
        }
        setData(list);
    }

    private void setData2(List<PickFilterEntity> list) {
        pickAdapter2.setDatas(list);
        pickAdapter2.resetCheckDatas();//切换数据 重置了之前选择的
        if (currentHistoryFirstItem != null){ //数据回显
            if (currentHistoryFirstItem.equals(currentFirstItem)){
                 if (!historyList.isEmpty()){
                     for (PickFilterEntity entity : list){
                         if (historyList.contains(entity)){
                             checkList.put(list.indexOf(entity), true);
                         } else {
                             checkList.put(list.indexOf(entity), false);
                         }
                     }
                     pickAdapter2.setCheckDefultDatas(historyList, checkList);
                 }
                 currentHistoryFirstItem = null;
            }
        }
    }

    public void setDefaultItem(int index) {
        this.currentPosition = index;
        if (list == null) return;
        setData(list);
    }

    private OnResultClick onResultClick;

    //是否可以只选一级后选中所有二级选项
    public void setIsOneSelete(boolean b) {
        this.isOneSelete = b;
    }

    public interface OnResultClick {

        void result(List<PickFilterEntity> list);
    }

    public void setOnResultClick(OnResultClick onResultClick) {
        this.onResultClick = onResultClick;
    }

    //如果是二级列表多选的话 获取第一级选择的数据
//    public List<PickFilterEntity> getProList(){
//        if (secondPickAdapter.getChooseList() == null || secondPickAdapter.getChooseList().isEmpty()
//         || list.isEmpty()){
//            return proList;
//        }
//        PickFilterEntity realEntity;
//        for (PickFilterEntity itemUnit : secondPickAdapter.getChooseList()){
//            for (PickFilterEntity<List<PickFilterEntity>> item : list){
//                if (item.getChildren() != null) {
//                    if (item.getChildren().contains(itemUnit)) {
//                        realEntity = new PickFilterEntity();
//                        realEntity.setNo(item.getNo());
//                        realEntity.setName(itemUnit.getNo());//保存备案单位的no
//                        proList.add(realEntity);
//                        break;
//                    }
//                }
//            }
//        }
//        return proList;
//    }
}
